<?php
function pg_last_inserted_id($con, $table){ 
        
        #make the initial query 
        $sql = "SELECT * FROM " . $table; 
        #execute 
        $ret = pg_query($con, $sql); 
        #get the field name 
        $campoId = pg_field_name($ret, 0); 
        
        #change the query, using currval() 
        $sql = "SELECT currval('".$table."_".$campoId."_seq')"; 
        
        #exec 
        $retorno =pg_query($con, $sql); 
        
        if(pg_num_rows($ret)>0){ 
            #array 
            $s_dados = pg_fetch_all($retorno); 
            
            #vars 
            extract($s_dados[0],EXTR_OVERWRITE); 
            
            return $currval; 
            
        } else { 
            #case error, returns false 
            return false; 
        } 
}